const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection,
	userCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let cUser = await userCollection.doc(payload.uid).get()
	cUser = cUser.data[0]
	//获取当前应用
	let appletInfoData = await appletInfoCollection.doc(cUser.app_id).get()
	appletInfoData = appletInfoData.data[0]
	if (params.money) {
		params.money = params.money * 100
	}
	if (params.yj_money) {
		params.yj_money = params.yj_money * 100
	}
	if (appletInfoData.wechat && !appletInfoData.wechat.gh_id) {
		return result_error('小程序原始ID为空')
	}
	if (!appletInfoData.wechat) {
		appletInfoData.wechat = {}
	}
	let checkData = params.checkData
	if (checkData) {
		let collectionData = await collectionCollection.where({
			app_id: appletInfoData._id,
			shop_id: appletInfoData._id,
			money: params.money,
			yj_money: params.yj_money
		}).get()
		collectionData = collectionData.data
		if (collectionData && collectionData.length > 0) {
			return result_error('已存在相同金额，相同押金收款单，直接分享收款更方便')
		}
	}
	params.wechat_gh_id = appletInfoData.wechat.gh_id
	params.wechat_id = appletInfoData.wechat.auth_app_id
	params.alipay_id = appletInfoData.alipay.auth_app_id
	params.app_id = appletInfoData._id
	params.shop_id = appletInfoData._id
	params.create_time = new Date().getTime()
	params.status = '进行中'
	params.total = 0
	params.count = 0
	await collectionCollection.add(params)
	return result_success()
}